package algocasts;

public class P110_RotateArray {
    // Time: O(n)  Space: O(n)
    public void rotate_Copy(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0) return;
        int n = nums.length, m = k % n, i = 0;
        int[] t = new int[n];
        for (int j = n - m; j < n; j++) t[i++] = nums[j];
        for (int j = 0; j < n - m; j++) t[i++] = nums[j];
        System.arraycopy(t, 0, nums, 0, n);
    }

    private void reverse(int[] nums, int left, int right) {
        for (; left < right; left++, right--) {
            int tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
        }
    }

    // Time: O(n)  Space: O(1)
    public void rotate_Swap(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0) return;
        int n = nums.length, m = k % n;
        reverse(nums, 0, n - 1);
        reverse(nums, 0, m - 1);
        reverse(nums, m, n - 1);
    }
}
